#include "TerrainManager.h"


void TerrainManager::TerrainBlock::Init( ID3D10Device* devcie,const TerrainInformation& info )
{
	m_terrain.Init(devcie,info);
	D3DXMatrixIdentity(&m_matWorld);
}

Terrain* TerrainManager::TerrainBlock::GetTerrain()
{
	return &m_terrain;
}

D3DXMATRIX& TerrainManager::TerrainBlock::GetWorldMatrix()
{
	return m_matWorld;
}

void TerrainManager::TerrainBlock::SetWorldMatrix( const D3DXMATRIX& world )
{
	m_matWorld = world;
}

void TerrainManager::TerrainBlock::SetBuffer()
{
	m_terrain.SetBuffer();
}

void TerrainManager::TerrainBlock::Destory()
{
	m_terrain.Destory();
}

TerrainManager::TerrainManager(void)
{
}


TerrainManager::~TerrainManager(void)
{
}

void TerrainManager::Rendering()
{
	TerrainInformation* pInfo;
	std::vector<TerrainBlock>::iterator iter;
	D3DXMATRIX world;
	D3DXMatrixIdentity(&world);

	m_TerrainEffect.BeginDraw();

	for(iter=m_TerrainList.begin() ;iter!=m_TerrainList.end() ;++iter)
	{
		pInfo = iter->GetTerrain()->GetTerrainInfomation();

		//set the buffer
		//m_TerrainEffect.SetVertexBuffer(iter->GetTerrain()->GetVertexBuffer());
		//m_TerrainEffect.SetIndexBuffer(iter->GetTerrain()->GetIndexBuffer());
		//iter->GetTerrain()->SetBuffer();
		iter->SetBuffer();

		//set the matrix
		m_TerrainEffect.SetWorldMatrix(world);
		m_TerrainEffect.SetWVPMatrix(world*GlobalMessage::Instance()->GetViewMatrix()
									*GlobalMessage::Instance()->GetProjMatrix());

		//set the texture
		m_TerrainEffect.SetLayer0(ResourcesManager::Instance()->GetTexture(pInfo->LayerMapFilename0));
		m_TerrainEffect.SetLayer1(ResourcesManager::Instance()->GetTexture(pInfo->LayerMapFilename1));
		m_TerrainEffect.SetLayer2(ResourcesManager::Instance()->GetTexture(pInfo->LayerMapFilename2));
		m_TerrainEffect.SetLayer3(ResourcesManager::Instance()->GetTexture(pInfo->LayerMapFilename3));
		m_TerrainEffect.SetLayer4(ResourcesManager::Instance()->GetTexture(pInfo->LayerMapFilename4));
		m_TerrainEffect.SetBlendMap(ResourcesManager::Instance()->GetTexture(pInfo->BlendMapFilename));

		//drawing
		m_TerrainEffect.Draw(iter->GetTerrain()->GetNumFaces());
	}
}

void TerrainManager::Init( ID3D10Device* devcie )
{
	m_pDevice = devcie;

	m_TerrainList.clear();
	m_TerrainEffect.Init(devcie);
}

void TerrainManager::AddTerrain( const TerrainInformation& info ,const D3DXVECTOR3& position )
{
	TerrainBlock tmpTerrain;

	tmpTerrain.Init(m_pDevice,info);

	D3DXMATRIX world,translate;
	D3DXMatrixIdentity(&world);
	D3DXMatrixTranslation( &translate, position.x, position.y, position.z );
	D3DXMatrixMultiply(&world,&world,&translate);

	tmpTerrain.SetWorldMatrix(world);

	m_TerrainList.push_back(tmpTerrain);
}

void TerrainManager::SetDirectionToSun( const D3DXVECTOR3& dir )
{
	D3DXVECTOR4 temp(dir.x, dir.y, dir.z, 0.0f);
	m_TerrainEffect.SetDirectionToSun(temp);
}

void TerrainManager::Destory()
{
	std::vector<TerrainBlock>::iterator iter;

	for(iter=m_TerrainList.begin() ;iter!=m_TerrainList.end() ;++iter)
		iter->Destory();
}
